<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="./css/reset.css" />
    <link rel="stylesheet" href="./css/iconfont.css" />
    <link rel="stylesheet" href="./css/main.css" />
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <script src="./js/jqconfig.js"></script>
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">文章类别管理</div>
      <div class="container-fluid common_con">
        <div class="row" style="margin-top: 10px">
          <div
            class="
              col-xs-offset-10
              col-sm-offset-10
              col-md-offset-10
              col-lg-offset-10
              col-xs-2
              col-sm-2
              col-md-2
              col-lg-2
            "
          >
            <a
              href="javascript:void(0)"
              class="btn btn-success"
              id="xinzengfenlei"
              data-toggle="modal"
              data-target="#myModal"
              >新增分类</a
            >
          </div>
        </div>

        <table
          class="
            table table-striped table-bordered table-hover
            mp20
            category_table
          "
        >
          <thead>
            <tr>
              <th>名称</th>
              <th>描述</th>
              <th class="text-center" width="100">操作</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>爱生活</td>
              <td>热爱生活</td>
              <td class="text-center">
                <button class="edit btn btn-info btn-xs">编辑</button>
                <button class="delete btn btn-danger btn-xs">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 新增分类布局区域-开始 -->
    <div
      class="modal fade"
      id="myModal"
      tabindex="-1"
      role="dialog"
      aria-labelledby="myModalLabel"
    >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">新增分类</h4>
          </div>
          <div class="modal-body">
            <form id="addform">
              <div class="add_category">
                <div class="form-group">
                  <label for="name">分类名</label>
                  <input
                    type="text"
                    class="form-control"
                    name="name"
                    placeholder="分类名"
                  />
                </div>
                <div class="form-group">
                  <label for="slug">分类别名</label>
                  <input
                    type="text"
                    class="form-control"
                    name="slug"
                    placeholder="分类别名"
                  />
                </div>
              </div>

              <div class="modal-footer">
                <button
                  type="button"
                  data-dismiss="modal"
                  class="btn btn-default"
                >
                  取消
                </button>
                <button type="submit" class="btn btn-primary btn_opt">
                  保存
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    <!-- 新增分类布局区域-结束 -->

    <!-- 编辑分类布局区域-开始 -->
    <div
      class="modal fade"
      id="editModal"
      tabindex="-1"
      role="dialog"
      aria-labelledby="myModalLabel"
    >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">编辑分类</h4>
          </div>
          <div class="modal-body">
            <form id="editform">
              <div class="add_category">
                <div class="form-group">
                  <label for="name">分类名</label>
                  <input
                    type="text"
                    class="form-control"
                    id="name"
                    placeholder="分类名"
                  />
                </div>
                <div class="form-group">
                  <label for="slug">分类别名</label>
                  <input
                    type="text"
                    class="form-control"
                    id="slug"
                    placeholder="分类别名"
                  />
                </div>
              </div>

              <div class="modal-footer">
                <button
                  type="button"
                  data-dismiss="modal"
                  class="btn btn-default"
                >
                  取消
                </button>
                <button
                  id="editSaveData"
                  type="button"
                  class="btn btn-primary btn_opt"
                >
                  保存
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
    <!-- 编辑分类布局区域-结束 -->
    <!-- 引入模态框 -->
    <script src="./libs/layui/layui.js"></script>
    <!-- 添加模板引擎 -->
    <script src="./libs/art-template/template-web.js"></script>
    <script id="tmp" type="text/html">
      {{each list }}
      <tr>
        <td>{{$value.name}}</td>
        <td>{{$value.slug}}</td>
        <td class="text-center">
          <button data-id="{{$value.id}}" class="edit btn btn-info btn-xs">
            编辑
          </button>
          <button
            data-id="{{$value.id}}"
            id="delete"
            class="delete btn btn-danger btn-xs"
          >
            删除
          </button>
        </td>
      </tr>
      {{/each}}
    </script>
    <script>
      $(function () {
        var layer;
        layui.use("layer", function () {
          layer = layui.layer;
          // layer.msg("hello");
          //获取数据
          getCateList();
          register_submit_for_addform();
        });
        //获取数据
        function getCateList() {
          $.ajax({
            type: "get",
            url: "/admin/category/list",
            success: (res) => {
              console.log(res);
              if (res.code !== 200) {
                layer.msg(res.msg, { time: 500 });
                return;
              }
              data = res.data;
              const htmlStr = template("tmp", { list: data });
              $("tbody").html(htmlStr);
            },
          });
        }
        //新增分类
        function register_submit_for_addform() {
          $("#addform").submit(function (e) {
            e.preventDefault();
            //序列化表单
            const fd = new FormData(document.querySelector("#addform"));
            const name = fd.get("name");
            const slug = fd.get("slug");
            //非空判断
            if (name.length == 0 || slug.length == 0) {
              return layer.msg("分类名或分类别名不能为空", { time: 500 });
            }
            // console.log(name, slug);
            $.ajax({
              type: "post",
              url: "/admin/category/add",
              data: { name, slug },
              success: (res) => {
                console.log(res);
                layer.msg(res.msg, { time: 500 }, function () {
                  //关闭模态框
                  $("#myModal").modal("hide");
                  //清空输入的表单数据
                  document.querySelector("#addform").reset();
                  //重新获取数据
                  getCateList();
                });
                $;
              },
              error: function (xhr) {
                layer.msg(xhr.responseJSON.msg, {
                  time: 2000, //2秒关闭（如果不配置，默认是3秒）
                });
              },
            });
          });
        }
        //删除类别
        $("tbody").on("click", ".delete", function () {
          const id = $(this).data("id");
          //删除确认
          // if (confirm("是否确认删除")) {
          //   //用户同意  就发送ajax请求数据
          //   $.ajax({
          //     type: "post",
          //     url: "/admin/category/delete",
          //     data: { id },
          //     success: (res) => {
          //       // console.log(res);
          //       //重新获取数据
          //       getCateList();
          //     },
          //   });
          // }
          //优化
          layer.confirm(
            "是否删除?",
            { icon: 3, title: "删除确认" },
            function (index) {
              //用户同意  就发送ajax请求数据
              $.ajax({
                type: "post",
                url: "/admin/category/delete",
                data: { id },
                success: (res) => {
                  // console.log(res);
                  //重新获取数据
                  getCateList();
                  layer.close(index);
                },
              });
            }
          );
        });

        //变量储存id
        let editId;
        //点击编辑 打开编辑模态框id :editModal 得到对应id的数据展示到模态框中
        $("tbody").on("click", ".edit", function () {
          $("#editModal").modal("show");
          //获取对应的id
          editId = $(this).data("id");
          // console.log(editId);
          //得到对应id的数据展示到模态框中
          $.ajax({
            type: "get",
            url: "/admin/category/search",
            data: { id: editId },
            success: (res) => {
              console.log(res);
              if (res.code !== 200) {
                layer.msg(res.msg, { time: 500 });
                return;
              }
              $("#name").val(res.data[0].name);
              $("#slug").val(res.data[0].slug);
            },
          });
        });

        //点击编辑模态框的保存  获取数据 传数据
        $("#editSaveData").click(function () {
          let name = $("#name").val();
          let slug = $("#slug").val();
          // console.log(name, slug);
          $.ajax({
            type: "post",
            url: "/admin/category/edit",
            data: {
              id: editId,
              name,
              slug,
            },
            success: (res) => {
              console.log(res);
              getCateList();
              $("#editModal").modal("hide");
            },
          });
        });
      });
    </script>
  </body>
</html>
